﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class Services_getRSQuality : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "uft-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

        string imageId = Request.QueryString[0].ToString();//选择的影像
        string rootId = Request.QueryString[1].ToString();//选择的省直辖市编号
        string distId = Request.QueryString[2].ToString();//选择的二级城市编号
        string[] ImageIdArray = imageId.Split('_');
        string samplingDateString = ImageIdArray[2] + "-" + ImageIdArray[3] + "-" + ImageIdArray[4];//选中的影像的日期
        string result = "";
        string SP_File_Name = "";
        DateTime ImageDate = new DateTime();

        string resultXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        try
        {
            string queryString = "select SP_FILE_NAME,IMAGEDATE from rs.DISTRICT_IMAGE_SP_REF where imageid like '%" + imageId + "%'";
            DataSet ds = OracleDataBase.Query(queryString);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count == 1)
            {
                SP_File_Name = dt.Rows[0][0].ToString();
                ImageDate = Convert.ToDateTime(dt.Rows[0][1].ToString());
            }
            else
            {
                resultXml += "<url>Error! Can't find your request image on server,please check again!</url>";
            }
        }
        catch (Exception ex)
        {
            resultXml += "<url>Error!" + ex.Message.ToString() + "</url>";
        }
        DataTable rs_dt = new DataTable();
        //if (distId != "" && distId != "1")
        //{
        //    string str = "select ndwi,ndwi2,ndvi,nri,nsi,wi,g_swir,g_swir2 from rs.sp_rsindex where sampdate=to_date('" + samplingDateString + "','yyyy-mm-dd') and distid=" + distId + "";
        //    DataSet q_ds = OracleDataBase.Query(str);
        //    rs_dt = q_ds.Tables[0];
        //}
        //if (distId == "1")
        //{
        //    string str = "select ndwi,ndwi2,ndvi,nri,nsi,wi,g_swir,g_swir2 from rs.sp_rsindex where sampdate=to_date('" + samplingDateString + "','yyyy-mm-dd') and distid=" + distId + "";
        //    DataSet q_ds = OracleDataBase.Query(str);
        //    rs_dt = q_ds.Tables[0];
        //}
        string str = "select POINTID,CRUDEPP,WETGP,SETTIME,NCONTENT from sp_quality_param ";
        DataSet q_ds = OracleDataBase.Query(str);
        rs_dt = q_ds.Tables[0];
        try
        {
            RemoteSensingIndex.RSI.ReSamplePointRemoteSensingIndex pRsi = new RemoteSensingIndex.RSI.ReSamplePointRemoteSensingIndex();

            if (pRsi.init())
            {

                string baseLocation = Server.MapPath("../");
                string sdeServer = System.Configuration.ConfigurationManager.AppSettings["SDE_SERVER"];
                string sdeInstance = System.Configuration.ConfigurationManager.AppSettings["SDE_INSTANCE"];
                string sdeUser = System.Configuration.ConfigurationManager.AppSettings["SDE_USER"];
                string sdePWD = System.Configuration.ConfigurationManager.AppSettings["SDE_PWD"];
                string OraConnString = System.Configuration.ConfigurationManager.ConnectionStrings["OracleConnectionString"].ToString();



                result = pRsi.GetIndexDataSet(SP_File_Name, imageId, baseLocation + "data\\", 3, OraConnString, sdeServer, sdeInstance, sdeUser, sdePWD, ImageDate);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(result);
                XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName("Parcel");
                int nChildren = xmlNodeList.Count;
                int nAttributes = xmlNodeList[0].ChildNodes.Count;

                resultXml += "<" + xmlDoc.ChildNodes[0].Name + ">" + "\n";
                for (int i = 0; i < nChildren; i++)
                {
                    resultXml += "<" + xmlNodeList[i].Name + " ";
                    for (int j = 0; j < nAttributes; j++)
                    {
                        resultXml += xmlNodeList[i].ChildNodes[j].Name + "='" + xmlNodeList[i].ChildNodes[j].FirstChild.Value + "' ";
                        foreach (DataRow row in rs_dt.Rows)
                        {
                            if (row["POINTID"].ToString() == xmlNodeList[i].ChildNodes[j].FirstChild.Value)
                            {
                                //添加数据到xml字符串中 
                                // " CRUDEPP ='" + row["CRUDEPP"].ToString() + "' WETGP ='" + row["WETGP"].ToString() + "' SETTIME ='" + row["SETTIME"].ToString() + "' NCONTENT ='" + row["NCONTENT"].ToString()+"'/>"+"\n"
                                resultXml += " CRUDEPP ='" + row["CRUDEPP"].ToString() + "' WETGP ='" + row["WETGP"].ToString() + "' SETTIME ='" + row["SETTIME"].ToString() + "' NCONTENT ='" + row["NCONTENT"].ToString() + "' ";

                            }
                        }

                    }
                    resultXml += " />" + "\n";
                }
                resultXml += "</" + xmlDoc.ChildNodes[0].Name + ">" + "\n";

            }
            else
            {
                resultXml += "<url>Error! Server Machine is out of license!</url>";
            }
        }
        catch (ExecutionEngineException ex)
        {
            resultXml += "<url>Error!" + ex.Message.ToString() + "</url>";
        }
        Response.Write(resultXml);
    }
}
